package com.demo.utils;

import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class CodeUtil {
   public static void main(String[] args) {
        /*String key = "12345678912mnk26563456789123r$t4h456789g47@r4ga5esg17rh4t4sh1r7hb14srgg4e";
        String password = "22541^$grhksth";*/
        String key = "gg4e";
        String password = "22541^$grhksthgragag";
       List<String> list = enCode(key, password);
        String s1 = deCode(key, Integer.valueOf(list.get(0)), list.get(1));
        System.out.println("密码：" + s1);
    }

    public static List<String> enCode(String key, String password){
        List<String> resultList = new ArrayList<>();
        String keyBase64 = Base64.getEncoder().encodeToString(key.toString().getBytes());
        String inKey = keyBase64.substring(0, 5);
        System.out.println("keyBase64：" + keyBase64);
        String passwordBase64 = Base64.getEncoder().encodeToString(password.toString().getBytes());
        System.out.println("passwordBase64：" + passwordBase64);
        char[] keyChars = keyBase64.toCharArray();
        ArrayList<String> keyList = new ArrayList<>();
        int keyL = keyChars.length;
        int passwordL = passwordBase64.length();
        if (passwordL < keyL){  //12aaa 34aaa 56aaa 7
            int b = keyL / passwordL;
            resultList.add(b + "");
            String lalala = "";
            for (int i = 0; i < keyChars.length; i++) {
                lalala = lalala + keyChars[i];
                if ((i + 1) % b == 0){
                    keyList.add(lalala  + inKey);
                    lalala = "";
                }
            }
            keyChars = null;
        }
        StringBuilder passwordBuilder = new StringBuilder(passwordBase64);
        StringBuilder result = new StringBuilder("");
        if (keyChars != null){
            int a = passwordL / keyL;
//            resultList.add(a + "");
            resultList.add(0 + "");
            for (int i = 0; i < keyChars.length; i++) {
                if (i == keyChars.length - 1){
                    result = result.append(new StringBuilder(passwordBuilder.substring(i * a, passwordBase64.length())).append(keyChars[i] + inKey));
                }else {
                    result = result.append(new StringBuilder(passwordBuilder.substring(i * a,(i+1) * a)).append(keyChars[i] + inKey));
                }
            }
        }else {
            String [] keyString = new String[keyList.size()];
            keyList.toArray(keyString);
            int a = passwordL / keyString.length;
            for (int i = 0; i < keyString.length; i++) {
                if (i == keyString.length - 1){
                    result = result.append(new StringBuilder(passwordBuilder.substring(i * a, passwordBase64.length())).append(keyString[i]));
                }else {
                    result = result.append(new StringBuilder(passwordBuilder.substring(i * a,(i+1) * a)).append(keyString[i]));
                }
            }
        }
//        System.out.println(result);
        String s = Base64.getEncoder().encodeToString(result.toString().getBytes());
        resultList.add(s);
//        System.out.println(s);
//        System.out.println(new String(Base64.getDecoder().decode(s)));
//        return result.toString();
        return resultList;
    }

    public static String deCode(String key, Integer size, String encodePassword){
        String passwordDecode = new String(Base64.getDecoder().decode(encodePassword));
        String keyBase64 = Base64.getEncoder().encodeToString(key.toString().getBytes());
        String inKey = keyBase64.substring(0, 5);
        ArrayList<String> keyList = new ArrayList<>();
        char[] keyChars = keyBase64.toCharArray();
        String lalala = "";
        if (size != 0){
            for (int i = 0; i < keyChars.length; i++) {
                lalala = lalala + keyChars[i];
                if ((i + 1) % size == 0){
                    keyList.add(lalala + inKey);
                    lalala = "";
                }
            }
        }else {
            for (int i = 0; i < keyChars.length; i++) {
                keyList.add(String.valueOf(keyChars[i]) + inKey);
            }
        }
        for (String s1 : keyList) {
            passwordDecode = passwordDecode.replace(s1, "");
        }
        String password = new String(Base64.getDecoder().decode(passwordDecode));
        return password;
    }
}
